import qs from "qs";
var adpatee = {
  GET: get,
  QUERY: query,
  PUT: put,
  IMAGE: image,
  PATCH: patch,
  PATCHQ: patchq,
  POST: post,
  DEFAULT: defaults,
  DELETE: delet,
};
function get(axios, url, data) {
  console.log(url)
  let paramStr = "";
  Object.keys(data).forEach((key) => {
    paramStr += key + "=" + data[key] + "&";
  });
  if (paramStr) {
    paramStr = paramStr.substring(0, paramStr.length - 1);
    url = url + "?" + paramStr;
  }
  //发送get请求
  return axios.get(url);
}

function query(axios, url, data) {
  url = url + `/${data.id}`;
  return axios.get(url);
}
function put(axios, url, data) {
  if (JSON.stringify(data) !== "{}") {
    url = url + `/${data.id}`;
  }
  return axios.put(url);
}
function defaults(axios, url, data) {
  url = url + `/${data.id}/default`;
  return axios.patch(url);
}
function delet(axios, url, data) {
  url = url + `/${data.id}`;
  return axios.delete(url);
}
function image(axios, url, data) {
  return axios.post(url, data, {
    headers: { "Content-Type": "multipart/form-data" },
  });
}

function patch(axios, url, data) {
  return axios.patch(url, data);
}
function patchq(axios, url, data) {
  url = url + `/${data.id}`;
  delete data.id;
  return axios.patch(url, data);
}
function post(axios, url, data) {
  return axios.post(url, qs.stringify(data));
}
function request(type, axios, url, data) {
  return adpatee[type](axios, url, data);
}
export default request;
